package stdio.costa.imoca.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;

public class IOUtils {

	public static void close(Closeable closeable) {
		if (closeable != null) {
			try {
				closeable.close();
			} catch (IOException e) {
			}
		}
	}

	public static byte[] read(InputStream input) throws IOException {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		int read = 0;
		byte[] data = new byte[1024];
		while ((read = input.read(data)) > -1) {
			output.write(data, 0, read);
		}
		return output.toByteArray();
	}

	public static byte[] readFully(InputStream input) throws IOException {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		int read = 0;
		byte[] data = new byte[1024];
		while ((read = input.read(data)) > -1) {
			output.write(data, 0, read);
		}
		return output.toByteArray();
	}
	
	public static ByteArrayInputStream[] asByteArrayInputStreams(byte[]... inputs) {
		ByteArrayInputStream[] result = new ByteArrayInputStream[inputs.length];
		for (int i = 0; i < inputs.length; i++) {
			result[i] = new ByteArrayInputStream(inputs[i]);
		}
		return result;
	}
}
